<?php

namespace app\admin\model;
use app\admin\validate\Banner as val_Banner;
use think\exception\ValidateException;
use think\Model;
class Banner extends Model
{
    protected $autoWriteTimestamp = 'datetime';
    protected $field = ['title','sort','status','path','create_time','thumb','type','bucket','types'];
  public static function  lists($typeId){

      $list=self::where(['types'=>$typeId])->select();



      return $list;
  }

    // 模型初始化
    protected static function init()
    {
        //TODO:初始化内容
    }

    public static function insertbanner(){
        try {

            $result = validate(val_Banner::class)->check(input(''));
            if(!empty($result)){
                $add['title']=input('title','','trim');
                $add['sort']=input('sort','','trim');
                $add['thumb']=input('thumb','','trim');
                $add['path']=input('path','','trim');
                $add['type']=input('type');
                $add['bucket']=input('bucket');
                $add['types']=input('types');
                $add['create_time']=date('Y-m-d H:i:s');
                $add['status']=input('status','','trim');
                $result=self::insert($add);
                if($result){
                    $data = array(
                        'code' => 200,
                        'msg' => '成功!',
                        'url'=>url('/nav/lists')->build()
                    );

                    return $data;
                }else{
                    $data = array(
                        'code' => 40002,
                        'msg' => '失败!',
                    );
                    return $data;
                }
                // dump($result);die;
            }
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $e->getError();
        }

    }

    public static function modifybanner(){
        try {

            $result = validate(val_Banner::class)->check(input(''));
            if (!empty($result)) {
                $add['title']=input('title','','trim');
                $add['sort']=input('sort','','trim');
                $add['thumb']=input('thumb','','trim');
                $add['path']=input('path','','trim');
                $add['type']=input('type');
                $add['bucket']=input('bucket');
                $add['types']=input('types');
                $add['create_time']=date('Y-m-d H:i:s');
                $add['status']=input('status','','trim');
                $result=self::where(['id'=>input('id')])->update($add);

                if(!empty($result)){
                    $data = array(
                        'code' => 200,
                        'msg' => '成功!',
                    );

                    return $data;
                }else{
                    $data = array(
                        'code' => 40002,
                        'msg' => '失败!',
                    );
                    return $data;
                }
                // dump($result);die;
            }
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息

            return $e->getError();
        }

    }


    public static function del(){

        $id=input('id');
        $res=self::where(['id'=>$id])->delete();
        if(!empty($res)){
            $msg=[
                'code'=>200,
                'msg'=>'success'
            ];
        }else{
            $msg=[
                'code'=>200,
                'msg'=>'删除失败'
            ];
        }

        return $msg;

    }


}